package site.codeblog.designpattern.singleton;

import java.io.Serializable;
import java.util.concurrent.locks.ReentrantLock;

public class Singleton implements Serializable {

    private static boolean flag = false;

    private Singleton() {
        if (flag){
            throw new RuntimeException("不允许创建多个实例");
        }
        flag = true;
    }

    //private static volatile Singleton instance;

    //private static final ReentrantLock lock = new ReentrantLock();

    private static class SingletonHolder{
        private static final Singleton INSTANCE = new Singleton();
    }

    public static Singleton getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public Object readResolve(){
        return getInstance();
    }
}
